# SPDX-License-Identifier: Apache-2.0
# Copyright (C) 2022-2024, Advanced Micro Devices, Inc. All rights reserved.

set(XDNA_SHIM_TEST shim_test.elf)

aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} MAIN_SOURCES)
add_executable(${XDNA_SHIM_TEST}
  ${MAIN_SOURCES}
  )

target_compile_definitions(${XDNA_SHIM_TEST} PRIVATE
  # below macros is required so that i/f defined in ishim.h is
  # consistent with native xrt implementation
  XRT_ENABLE_AIE
  XRT_BUILD
  )

target_link_libraries(${XDNA_SHIM_TEST} PRIVATE
  xrt_coreutil # for xclbin parser and some other helpers
  aiebu_static
  dl
  )

set_target_properties(${XDNA_SHIM_TEST} PROPERTIES
  BUILD_WITH_INSTALL_RPATH FALSE
  LINK_FLAGS "-Wl,-rpath,$ORIGIN/../lib"
  )

target_include_directories(${XDNA_SHIM_TEST} PRIVATE
  ${XRT_SUBMOD_SOURCE_DIR}/src/runtime_src
  ${XRT_SUBMOD_SOURCE_DIR}/src/runtime_src/core/include
  ${XRT_SUBMOD_BINARY_DIR}/src/gen
  )

target_compile_options(${XDNA_SHIM_TEST} PRIVATE -O3)

configure_file(
  shim_test.in
  shim_test
  @ONLY
  )

install(TARGETS ${XDNA_SHIM_TEST} DESTINATION ${XDNA_BIN_DIR}/bin)
install(PROGRAMS ${CMAKE_CURRENT_BINARY_DIR}/shim_test DESTINATION ${XDNA_BIN_DIR}/bin)

install(CODE "execute_process( \
  COMMAND ${CMAKE_COMMAND} -E create_symlink \
  ${AMDXDNA_BINS_DIR}/workspaces/NPU1_1x4_TESTCASES \
  ${CMAKE_BINARY_DIR}/${XDNA_BIN_DIR}/npu1_workspace \
  )"
  )

install(CODE "execute_process( \
  COMMAND ${CMAKE_COMMAND} -E create_symlink \
  ${AMDXDNA_BINS_DIR}/workspaces/NPU2_1x4_TESTCASES \
  ${CMAKE_BINARY_DIR}/${XDNA_BIN_DIR}/npu2_workspace \
  )"
  )

install(CODE "execute_process( \
  COMMAND ${CMAKE_COMMAND} -E create_symlink \
  ${AMDXDNA_BINS_DIR}/workspaces/NPU3_1x4_TESTCASES \
  ${CMAKE_BINARY_DIR}/${XDNA_BIN_DIR}/npu3_workspace \
  )"
  )

install(CODE "execute_process( \
  COMMAND ${CMAKE_COMMAND} -E create_symlink \
  ${AMDXDNA_BINS_DIR}/workspaces/NPU4_1x4_TESTCASES \
  ${CMAKE_BINARY_DIR}/${XDNA_BIN_DIR}/npu4_workspace \
  )"
  )

install(CODE "execute_process( \
  COMMAND ${CMAKE_COMMAND} -E create_symlink \
  ${AMDXDNA_BINS_DIR}/workspaces/NPU5_1x4_TESTCASES \
  ${CMAKE_BINARY_DIR}/${XDNA_BIN_DIR}/npu5_workspace \
  )"
  )

install(CODE "execute_process( \
  COMMAND ${CMAKE_COMMAND} -E create_symlink \
  ${AMDXDNA_BINS_DIR}/workspaces/NPU6_1x4_TESTCASES \
  ${CMAKE_BINARY_DIR}/${XDNA_BIN_DIR}/npu6_workspace \
  )"
  )

# Set up symlink for local shim test case data
set(XDNA_SHIM_TEST_DATA_DIR local_shim_test_data)
install(CODE "execute_process( \
  COMMAND ${CMAKE_COMMAND} -E create_symlink \
  ${AMDXDNA_BINS_DIR}/${XDNA_SHIM_TEST_DATA_DIR} \
  ${CMAKE_BINARY_DIR}/${XDNA_BIN_DIR}/${XDNA_SHIM_TEST_DATA_DIR} \
  )"
  )

# Set up symlink for preemption libs generated by aiebu
add_custom_target(find_preemption_libs ALL
  COMMAND ${CMAKE_COMMAND}
  -DSEARCH_PATH=${XRT_SUBMOD_BINARY_DIR}
  -DBIN_DIR=${CMAKE_BINARY_DIR}/${XDNA_BIN_DIR}
  -P ${CMAKE_SOURCE_DIR}/CMake/install_preemption_libs.cmake
  DEPENDS ${XDNA_SHIM_TEST}
  )
